package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.CommonMsgNameConstants;
import com.tsd.CommonParamNameConstants;
import com.tsd.common.controller.BaseController;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysDataObj;
import com.tsd.system.entity.SysDataObjExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysDataObjService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysDataObj数据对象 控制层
 *
 * @author david
 * @date 2021/07/08 15:49:18
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "系统-数据对象", description = "数据对象的API描述")
public class SysDataObjController extends BaseController {

    @Resource
    private SysDataObjService sysDataObjService;

    @ApiOperation(value = "根据id获取数据对象", notes = "根据id获取数据对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据对象id", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getSysDataObjBySid.action", method = RequestMethod.POST)
    public HttpResult<Object> getSysDataObjBySid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, CommonParamNameConstants.SID);
            super.checkEmpty(sid, CommonMsgNameConstants.EMPTY_MSG_SID);
            SysDataObj sysDataObj = sysDataObjService.getSysDataObjBySid(sid);
            hr.setData(sysDataObj);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除数据对象", notes = "根据id删除数据对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据对象id", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteSysDataObj.action", method = RequestMethod.POST)
    public HttpResult<Object> deleteSysDataObj() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get(CommonParamNameConstants.SIDS);
            super.checkEmpty(sids, CommonMsgNameConstants.EMPTY_MSG_SIDS);
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            sysDataObjService.deleteSysDataObj(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存数据对象", notes = "保存数据对象，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "数据对象", required = true, paramType = "body", dataType = "SysDataObj")
    })
    @ResponseBody
    @RequestMapping(value = "/saveSysDataObj.action", method = RequestMethod.POST)
    public HttpResult<Object> saveSysDataObj() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            SysDataObj t = JSON.parseObject(json, SysDataObj.class);
            sysDataObjService.saveSysDataObj(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询数据对象", notes = "分页查询数据对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findSysDataObjs.action", method = RequestMethod.POST)
    public HttpResult<Object> findSysDataObjs() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>();
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.SEARCH_VALUE, false, false);
            PageBean page = new PageBean(map);
            List<SysDataObjExt> list = sysDataObjService.findSysDataObjs(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ResponseBody
    @RequestMapping(value = "/findSysDataObjAccessRuleList.action", method = RequestMethod.POST)
    public HttpResult<Object> findSysDataObjAccessRuleList() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> param = new HashMap<>();
            List<SysDataObjExt> list = sysDataObjService.findSysDataObjAccessRuleList(param);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
